package basic.JUC.print100;

/**
 * @创建人 MBY381
 * @创建时间 2022/4/13 18:25
 * @描述 暂无
 */
public class EnumCAS_Print {
    enum ReadyToRun{
        T1,T2
    }
    private static volatile ReadyToRun readyToRun = ReadyToRun.T1;

    public static void main(String[] args) {
        char[] a1 = "1234567".toCharArray();
        char[] a2 = "ABCDEFG".toCharArray();

        new Thread(() ->{
            for(char c : a1){
                while (readyToRun != ReadyToRun.T1){}//cas自旋
                System.out.println(c);
                readyToRun = ReadyToRun.T2;//线程可见性
            }
        },"t1").start();

        new Thread(() ->{
            for(char c : a2){
                while (readyToRun != ReadyToRun.T2){}//cas自旋
                System.out.println(c);
                readyToRun = ReadyToRun.T1;//线程可见性
            }
        },"t2").start();
    }
}
